/**
*  \file direction_switch.h
*
*  \brief State machine for direction switch.
*
*  \author Michael J. Hohmann  michael.hohmann@linde-mh.de
*
*
*/
#ifndef DIRECTION_SWITCH_H_INCLUDED
#define DIRECTION_SWITCH_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "../cfg/prj.h"

/*================================================[ public defines   ]================================================*/

/*================================================[ public datatypes ]================================================*/
enum direction_switch_state
{
   direction_switch_state_neutral  ,
   direction_switch_state_forward  ,
   direction_switch_state_reverse  ,
   direction_switch_state_not_ready,
   direction_switch_state_invalid
};


enum direction_switch_error
{
   direction_switch_error_fw_and_bw_acktive,
   direction_switch_error_fw_and_n_acktive,
   direction_switch_error_bw_and_n_acktive,
   direction_switch_error_no_signal,
   direction_switch_error_none
};


/** Object definition.
* 
* The struct direction_switch_obj defines the attribute of an object.
*/
struct direction_switch_obj
{	
   enum direction_switch_state current_state;
   enum direction_switch_error current_error;
   U16 ticks_in_current_error;
   U16 ticks_in_current_state;
   U16 tick_count;
   S16 fwd_tick_count;
   S16 bwd_tick_count;
   S16 ntl_tick_count;
};

/*================================================[ public variables ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/

/**
*  Constructor.
*
*  Construct an object. 
*
*  \param [in,out] me Object pointer. NULL will throw an assert.
*
*  \return RC_SUCCESS if successful.
*/
RC direction_switch_construct( struct direction_switch_obj *me );

/** Process signals
*
*  This function should be called cyclicly (e.g. every 10 ms)
*
*  \param [in, out] me Object pointer. NULL will throw an assert.
*  \param [in     ] fwd Forward signal
*  \param [in     ] bwd backward signal
*  \param [in     ] neutral neutral signal
*
*/
void direction_switch_process( struct direction_switch_obj *me, BO fwd, BO neutral,BO bwd  );


/** Get current direction
*
*
*  \param [in]  me Object pointer. NULL will throw an assert.
*  \param [out] p_state_time Number of ticks the current state is active. It's save to pass NIL if the value is not needed.
*
*  \return The current state
*/
enum direction_switch_state direction_switch_get_state( struct direction_switch_obj const * me, U16 *p_state_time );

/** Get current error
*
*
*  \param [in]  me Object pointer. NULL will throw an assert.
*  \param [out]  p_error_time Number of ticks the current error is active. It's save to pass NIL if the value is not needed.
*
*  \return The current error
*/
enum direction_switch_error direction_switch_get_error( struct direction_switch_obj const * me, U16 *p_error_time );









/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
*                             Copyright 2019 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
